package com.dao;

import org.apache.log4j.Logger;
import org.hibernate.Query;

import com.base.PageBean;
import com.bean.ParkOrder;
import com.bean.ParkStatBean;
import com.bean.Parkinfo;
import com.base.BaseDao;
import com.tool.DateTimeTools;
import com.util.GUtil;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class ParkStatDao extends BaseDao{
	private static final Logger log = Logger.getLogger(ParkOrderDao.class);
	/*
	 * 列表统计各个停车场的收入额
	 */
	public List<ParkStatBean> moneylist(String dateTime, PageBean page,ParkOrder order){
		session = this.getSession();
		//列表显示
		String sql="select new com.bean.ParkStatBean((select o.PName from Parkinfo o where o.PId=p.parkinfo.PId),count(*)," +
				"(select count(*) from ParkOrder  where status=20),sum(p.receivableAmount),sum(p.actualAmount))" +
				" from ParkOrder p where p.poType=1 and p.leaveTime like '"+dateTime+"%' " +
				"GROUP BY p.parkinfo.PId";
		Query queryObject=session.createQuery(sql);
		List<ParkStatBean> list1=queryObject.list();
		return list1;
	}
	public List<ParkStatBean> moneylistzhou(String dateTime, PageBean page,ParkOrder order) throws ParseException{
		session = this.getSession();
		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
		//String weektime = "2014-05-06";
		Date timelong = formatDate.parse(dateTime);
		Timestamp inTime = new Timestamp(timelong.getTime());
		Timestamp outStartTime = new Timestamp(timelong.getTime());
		Timestamp outEndTime = new Timestamp(timelong.getTime());
		DateTimeTools.getWeekDays(inTime, outStartTime, outEndTime);//获取月的开始和结束时间，从而获取本月的最后一天，即获取本月的长度

		//列表显示
		String sql="select new com.bean.ParkStatBean((select o.PName from Parkinfo o where o.PId=p.parkinfo.PId),count(*)," +
				"(select count(*) from ParkOrder  where status=20),sum(p.receivableAmount),sum(p.actualAmount))" +
				" from ParkOrder p where p.poType=1 and p.leaveTime between ? and ? " +
				" GROUP BY p.parkinfo.PId";
		Query query=session.createQuery(sql);
		query.setTimestamp(0, outStartTime);
		query.setTimestamp(1, outEndTime);
		List<ParkStatBean> list1=query.list();
		return list1;
	}
	/*
	 * 年
	 */
	public Long[][] queryByYearo(List<Parkinfo> payInOut,List<Parkinfo> payStat,
			Map<Double, Double> paylistInOutByDay, String yeartime, Long userId)
					throws Exception {
			int enddate = 12;// outEndTime.getDate();
			Long[][] data = new Long[2][enddate];
			for(int i=0; i<2; i++){
				for(int j=0; j<enddate; j++){
					if(i==0)
						data[i][j] = (long)(j+1);
					else
						data[i][j] = (long) 0;
				}
			}
			session = this.getSession();
			String hql = "select new com.bean.Parkinfo(year(createTime),month(createTime),count(*),status) from Parkinfo" +
					" where createTime like '"+yeartime+"%'"+
					" GROUP BY year(createTime),month(createTime) ";
			Query query = session.createQuery(hql);
			List<Parkinfo> list = query.list();
			if(list!=null){
				for(Parkinfo par : list){
					int x = GetIndexByStatus(par.getStatus());
					int y=par.getMonth()-1;
					if(x==1 && y<enddate && y>=0){
						data[x][y] = Math.abs(par.getCountOrder());
					}
				}
			}else{
				return null;
			}
			log.info(this.getClass()+".queryByYear()"+  list);
			return data;
	}

	public int GetIndexByStatus(int status){
		//-2：暂停营业中；-1：已删除；0：已冻结；1：未审核；2：审核中；3：正常
		switch(status){
		case -2:
			return 1;
		case -1:
			return 1;
		case 0:
			return 1;
		case 1:
			return 1;
		case 2:
			return 1;
		case 3:
			return 1;
		}
		return -100;
	}
	/*
	 * 月
	 */
	public Long[][] queryByMontho(List<Parkinfo> payInOut,List<Parkinfo> payStat,
			Map<Double, Double> paylistInOutByDay, String monthtime, Long userId)
					throws Exception {
		{
			session = this.getSession();
			SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM");
			//String datetime = "2014-05";
			Date timelong = formatDate.parse(monthtime);
			Timestamp inTime = new Timestamp(timelong.getTime());
			Timestamp outStartTime = new Timestamp(timelong.getTime());
			Timestamp outEndTime = new Timestamp(timelong.getTime());
			DateTimeTools.getMonthDays(inTime, outStartTime, outEndTime);//获取月的开始和结束时间，从而获取本月的最后一天，即获取本月的长度
			//按day分类查询本月的折线图数据
			int enddate = outEndTime.getDate();
			Long[][] data1 = new Long[2][enddate];
			for(int i=0; i<2; i++){
				for(int j=0; j<enddate; j++){
					if(i==0)
						data1[i][j] = (long)(j+1);//日期，天
					else
						data1[i][j] = (long) 0;
				}
			}
			String hql = "select new com.bean.Parkinfo(year(createTime),MONTH(createTime),day(createTime),count(*),status) from Parkinfo" +
					" where createTime like '"+monthtime+"%'"+
					" GROUP BY year(createTime),month(createTime),day(createTime)";
			Query query = session.createQuery(hql);
			List<Parkinfo> list = query.list();
			if(list!=null){
				for(Parkinfo par : list){
					int x= GetIndexByStatus(par.getStatus());
					int y= par.getDay()-1;
						//data1[x][y] = Math.abs(par.getCountOrder());
						if(x==1 && y<enddate && y>=0){
							data1[x][y] = Math.abs(par.getCountOrder());
						}
				}
			}else{
				return null;
			}
			log.info( data1.toString());
			return data1;
		}
	}
	/*
	 * 周
	 */
	public Long[][] queryByWeeko(List<Parkinfo> payInOut,List<Parkinfo> payStat,
			Map<Double, Double> paylistInOutByDay, String weektime, Long userId)
					throws Exception {

		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
		//String weektime = "2014-05-06";
		Date timelong = formatDate.parse(weektime);
		Timestamp inTime = new Timestamp(timelong.getTime());
		Timestamp outStartTime = new Timestamp(timelong.getTime());
		Timestamp outEndTime = new Timestamp(timelong.getTime());
		DateTimeTools.getWeekDays(inTime, outStartTime, outEndTime);//获取月的开始和结束时间，从而获取本月的最后一天，即获取本月的长度

		//查询 payInOut、payStat
		 	
		 
		session = this.getSession();
		
		

		//统计7天数据
		//query paylistInOutByDay
		int enddate = 7;// outEndTime.getDate();
		Long[][] data = new Long[2][enddate];
		for(int i=0; i<2; i++){
			for(int j=0; j<enddate; j++){
				if(i==0)
					data[i][j] = (long)(j+1);
				else
					data[i][j] = (long) 0;
			}
		}
		String hql = "select new com.bean.Parkinfo(year(createTime),MONTH(createTime),day(createTime),count(*),status) from Parkinfo"
				+ " where createTime between ? and ? group by year(createTime),MONTH(createTime),day(createTime)";
		Query query = session.createQuery(hql);
		query.setTimestamp(0, outStartTime);
		query.setTimestamp(1, outEndTime);
		List<Parkinfo> list1 = query.list();
		if(list1!=null){
			for(Parkinfo par : list1){
				int x = GetIndexByStatus(par.getStatus());
				int y=par.getDay()-outStartTime.getDate();
					data[x][y] = Math.abs(par.getCountOrder());
			}
		}else{
			return null;
		}
		log.info( this.getClass()+".queryByWeek() "+data.toString());
		return data;
		
	}
	/*
	 * 日
	 */
	public String queryByTimeo(List<Parkinfo> payInOut,List<Parkinfo> payStat,
			String daytime, Long userId)
			throws Exception {

		session = this.getSession();
		String hql = "select new com.bean.Parkinfo(year(createTime),MONTH(createTime),day(createTime),count(*),status) from Parkinfo"
				+ " and createTime like '"
				+ daytime + "%' group by year(createTime),MONTH(createTime),day(createTime)";
		Query query = session.createQuery(hql);
		List<ParkOrder>  list= query.list();	
		log.info("payInOut.size=" + payInOut.size());
		log.info("payStat.size=" + payStat.size());
		return GUtil.SUCCESS;
	}
	/*
	 *统计
	 *按天查询订单的数量
	 * 
	 */
	public String queryByDayo(List<Parkinfo> payInOut,List<Parkinfo> payStat,
			String daytime, Long userId) throws Exception {
		return this.queryByTimeo(payInOut,payStat,daytime, userId);
	}
}
